const db = require('../db/db');

class Task {
    static async getAll(filters = {}) {
        return new Promise((resolve, reject) => {
            let query = 'SELECT * FROM tasks';
            const params = [];
            const conditions = [];

            // 处理搜索关键词
            if (filters.q) {
                conditions.push('(title LIKE ? OR description LIKE ?)');
                params.push(`%${filters.q}%`, `%${filters.q}%`);
            }

            // 处理优先级筛选
            if (filters.priority) {
                conditions.push('priority = ?');
                params.push(filters.priority);
            }

            // 处理状态筛选
            if (filters.status) {
                conditions.push('status = ?');
                params.push(filters.status);
            }

            if (conditions.length > 0) {
                query += ' WHERE ' + conditions.join(' AND ');
            }

            db.all(query, params, (err, rows) => {
                if (err) reject(err);
                else resolve(rows);
            });
        });
    }

    static async create(taskData) {
        const { id, title, description, priority, status, start_date, due_date } = taskData;
        // 转换日期格式
        const formatDate = (dateStr) => dateStr ? dateStr.split('T')[0] : null;

        return new Promise((resolve, reject) => {
            db.run(
                'INSERT INTO tasks (...) VALUES (?, ?, ?, ?, ?, ?, ?)',
                [
                    id,
                    title,
                    description,
                    priority,
                    status,
                    formatDate(start_date),  // 转换开始日期
                    formatDate(due_date)    // 转换截止日期
                ],
                function (err) {
                    if (err) reject(err);
                    else resolve(this.lastID);
                }
            );
        });
    }

    static async update(id, updateData) {
        return new Promise((resolve, reject) => {
            const fields = [];
            const values = [];

            for (const [key, value] of Object.entries(updateData)) {
                fields.push(`${key} = ?`);
                values.push(value);
            }
            values.push(id);

            db.run(
                `UPDATE tasks SET ${fields.join(', ')} WHERE id = ?`,
                values,
                function (err) {
                    if (err) reject(err);
                    else resolve(this.changes);
                }
            );
        });
    }

    static async delete(id) {
        return new Promise((resolve, reject) => {
            db.run('DELETE FROM tasks WHERE id = ?', [id], function (err) {
                if (err) reject(err);
                else resolve(this.changes);
            });
        });
    }

    static async updateStatus(id, status) {
        return new Promise((resolve, reject) => {
            db.run(
                'UPDATE tasks SET status = ? WHERE id = ?',
                [status, id],
                function (err) {
                    if (err) reject(err);
                    else resolve(this.changes);
                }
            );
        });
    }

    static async getById(id) {
        return new Promise((resolve, reject) => {
            db.get(
                'SELECT * FROM tasks WHERE id = ?',
                [id],
                (err, row) => {
                    if (err) reject(err);
                    else resolve(row || null);
                }
            );
        });
    }

    static async updateFeedback(id, feedback) {
        return new Promise((resolve, reject) => {
            db.run(
                'UPDATE tasks SET feedback = ? WHERE id = ?',
                [feedback, id],
                function (err) {
                    if (err) reject(err);
                    else resolve(this.changes);
                }
            );
        });
    }

    static async updatePriority(id, priority) {
        return new Promise((resolve, reject) => {
            db.run(
                'UPDATE tasks SET priority = ? WHERE id = ?',
                [priority, id],
                function (err) {
                    if (err) reject(err);
                    else resolve(this.changes);
                }
            );
        });
    }
}

// 添加模块导出语句
module.exports = Task; // 这是缺失的关键导出语句